package com.dein.user.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.dein.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.dein.user.service.UserService;

@Controller
@RequestMapping("user/")
public class UserController {
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    @Autowired
    @Qualifier("userService")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "userLogin", method = RequestMethod.POST)
    public
    @ResponseBody
    Object userLogin(
            @RequestParam(value = "userName")String userName,
            @RequestParam(value = "userPw")String userPw,
            HttpServletRequest request) throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        Long userId = userService.userLogin(userName, userPw);
        if (userId == null) {
            //查询出现了错误  或  用户名或密码出现错误
            map.put("result", false);
            return map;
        }
        User user = userService.getUserById(userId);
        //查询出现了错误
        if( user == null){
            map.put("result", false);
            return map;
        }
        request.getSession().setAttribute("userName",userName);
        request.getSession().setAttribute("userId",userId);
        request.getSession().setAttribute("user",user);
        //说明登录成功
        map.put("result",true);
        return map;
    }

    @RequestMapping(value = "getUserById", method = RequestMethod.GET)
    public
    @ResponseBody
    Object getUserById(
            @RequestParam(value = "userId") Long userId) {

        User user = userService.getUserById(userId);
        return user;
    }

    @RequestMapping(value = "updateUser",method = RequestMethod.POST)
    public @ResponseBody Object updateUser(User user){
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println(user.getUserPw());
        boolean result = userService.updateUser(user);
        if(!result){
            map.put("result", false);
            return map;
        }
        map.put("result", true);
        return map;
    }

    @RequestMapping(value = "userExist",method = RequestMethod.POST)
    public @ResponseBody Object userExist(
            @RequestParam(value = "userName")String userName){
        Map<String, Object> map = new HashMap<String, Object>();
        boolean result = userService.userExist(userName);
        if(!result){
            //用户名不存在或者查询错误
            map.put("result", false);
            return map;
        }
        //用户名已存在
        map.put("result", true);
        return map;
    }

    @RequestMapping(value = "userRegist",method = RequestMethod.POST)
    public  @ResponseBody Object userRegist(HttpServletRequest request,
                                            User user){
        Map<String, Object> map = new HashMap<String, Object>();
        boolean result = userService.userRegist(user);
        if(!result){
            map.put("result", false);
            return map;
        }
        request.getSession().setAttribute("userName",user.getUserName());
        request.getSession().setAttribute("userId",user.getUserId());
        request.getSession().setAttribute("user",user);
        //说明注册成功
        map.put("result", true);
        return map;
    }

    @RequestMapping(value = "userExit",method = RequestMethod.GET)
    public String userExit(HttpServletRequest request){
        request.getSession().removeAttribute("userName");
        request.getSession().removeAttribute("userId");
        request.getSession().removeAttribute("user");
        return "main";
    }
}
